home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / perl5 / Text / Tabs.pm < prev   
Text File  |  1995-07-02  |  677b  |  48 lines

  1. #
  2. # expand and unexpand tabs as per the unix expand and 
  3. # unexpand programs.
  4. #
  5. # expand and unexpand operate on arrays of lines.  Do not
  6. # feed strings that contain newlines to them.
  7. #
  8. # David Muir Sharnoff <muir@idiom.com>
  9.  
  10. package Tabs;
  11.  
  12. require Exporter;
  13.  
  14. @ISA = (Exporter);
  15. @EXPORT = qw(expand unexpand $tabstop);
  16.  
  17. $tabstop = 8;
  18.  
  19. sub expand
  20. {
  21.     my @l = @_;
  22.     for $_ (@l) {
  23.         1 while s/^([^\t]*)(\t+)/
  24.             $1 . (" " x 
  25.                 ($tabstop * length($2)
  26.                 - (length($1) % $tabstop)))
  27.             /e;
  28.     }
  29.     return @l;
  30. }
  31.  
  32. sub unexpand
  33. {
  34.     my @l = &expand(@_);
  35.     my @e;
  36.     for $x (@l) {
  37.         @e = split(/(.{$tabstop})/,$x);
  38.         for $_ (@e) {
  39.             s/  +$/\t/;
  40.         }
  41.         $x = join('',@e);
  42.     }
  43.     return @l;
  44. }
  45.  
  46. 1;
  47.